package com.hns.can.service.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.hns.can.common.web.BaseController;
import com.hns.can.service.entity.CommCarCurrentLoInfoEntity;
import com.hns.can.service.service.VehicleOnLineService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:zhangy
 * @version Revision 1.0.0
 * @email:zhangyu2222z@sina.cn
 * @see:
 * @创建日期：2015年7月20日
 * @功能说明：车辆在线情况controller
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2015年7月20日  	       zhangy       	创建
 * @end
 */
@Controller
@RequestMapping("/vehicleOnLine")
public class VehicleOnlineStatusController extends BaseController {

    @Autowired
    private VehicleOnLineService vehicleOnLineService;

	/**
	 * 导出文件表头
	 */
	private final String[] tableHeaderNames={"所属机构","运营线路","车牌号","设备ID","SIM卡号","最后版本升级日期", "最后升级版本","最后在线时间","最后在线位置","离线时长（小时）"};
	/**
	 * 导出文件字段名
	 */
	private final String[] tableHeaderFields={"organName","lineName","licPltNo", "eqmtCd","simCardno","lastUpdateDate","vrsnName","rcrdTime","detailAdr","offlineHours"};

	/**
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 * @创建日期:2015年7月20日
	 * @功能说明：查询车辆在线情况
	 */
	@RequestMapping("/searchVehicleOnLineStatus")
	@ResponseBody
	public Object searchVehicleOnLineStatus(CommonParamsVo vo) throws Exception {
		if(PubUtil.isNotEmpty(vo.getCarIds())){
			List<String> orgIds = Arrays.asList(vo.getCarIds());
			if(PubUtil.isNotEmpty(orgIds)){
				vo.setOrgIds(orgIds);
			}
		}else {
			if (PubUtil.isNotEmpty(vo.getLineIds())) {
				List<String> orgIds = Arrays.asList(vo.getLineIds());
				if (PubUtil.isNotEmpty(orgIds)) {
					vo.setOrgIds(orgIds);
				}
			}
		}
		if(PubUtil.isEmpty(vo.getOrganIds()) || vo.getOrganIds().length<=0){
			vo.setOrganIds(null);
		}
		vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
		Pagination page = vehicleOnLineService.searchvehicleOnLineStatus(vo);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
	/**
	 * @param vo
	 * @param response
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 * @创建日期:2015年7月20日
	 * @功能说明：导出车辆在线情况excel
	 */
	@RequestMapping("/exportVehicleOLStatusExcel")
	public void exportVehicleOLStatusExcel(CommonParamsVo vo,HttpServletResponse response) throws Exception {
		if(PubUtil.isNotEmpty(vo.getCarIds())){
			List<String> orgIds = Arrays.asList(vo.getCarIds());
			if(PubUtil.isNotEmpty(orgIds)){
				vo.setOrgIds(orgIds);
			}
		}else {
			if (PubUtil.isNotEmpty(vo.getLineIds())) {
				List<String> orgIds = Arrays.asList(vo.getLineIds());
				if (PubUtil.isNotEmpty(orgIds)) {
					vo.setOrgIds(orgIds);
				}
			}
		}
		if(PubUtil.isEmpty(vo.getOrganIds()) || vo.getOrganIds().length<=0){
			vo.setOrganIds(null);
		}
		vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
		List<CommCarCurrentLoInfoEntity> list = vehicleOnLineService.getVehicleOLList(vo);
		ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", URLEncoder.encode("车辆在线历史明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆在线历史明细");
	}


	/**
	 * @param vo
	 * @param response
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 * @创建日期:2015年7月20日
	 * @功能说明：导出车辆在线情况pdf
	 */
	@RequestMapping("/exportVehicleOLStatusPdf")
	public void exportVehicleOLStatusPdf(CommonParamsVo vo,HttpServletResponse response) throws Exception {
		if(PubUtil.isNotEmpty(vo.getCarIds())){
			List<String> orgIds = Arrays.asList(vo.getCarIds());
			if(PubUtil.isNotEmpty(orgIds)){
				vo.setOrgIds(orgIds);
			}
		}else {
			if (PubUtil.isNotEmpty(vo.getLineIds())) {
				List<String> orgIds = Arrays.asList(vo.getLineIds());
				if (PubUtil.isNotEmpty(orgIds)) {
					vo.setOrgIds(orgIds);
				}
			}
		}
		if(PubUtil.isEmpty(vo.getOrganIds()) || vo.getOrganIds().length<=0){
			vo.setOrganIds(null);
		}
		vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
		List<CommCarCurrentLoInfoEntity> list = vehicleOnLineService.getVehicleOLList(vo);
		ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", URLEncoder.encode("车辆在线历史明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆在线历史明细");
	}

}
